//
//  ManyDayTitleView.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/2.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "ManyDayTitleView.h"
#import "UILabel+BasicAttributes.h"
#import "UIButton+BaseAttributes.h"




#define NormalColor [UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f]
#define SelectColor [UIColor colorWithRed:0.21f green:0.50f blue:0.90f alpha:1.00f]

@interface ManyDayTitleView()
//<UIScrollViewDelegate>
//@property (nonatomic,strong)UIScrollView *titleScroller;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UIButton *operationBtn;
@property (nonatomic,strong)UIButton *vehicelBtn;
@property (nonatomic,strong)UIButton *driveBtn;
@property (nonatomic,strong)UIButton *alarmBtn;
@end


@implementation ManyDayTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
//    self = [UIScrollView new];
//    self.delegate = self;
//    self.showsVerticalScrollIndicator = NO;
//    self.showsHorizontalScrollIndicator = NO;
//    self.bounces = NO;
//    self.contentSize = CGSizeMake(kWidthOfPt(310)*4, 0);
//    [self addSubview:self];
//    [self mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.bottom.mas_equalTo(self);
//    }];
    
    _operationBtn = [UIButton bttonWithTitle:@"   " andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    _operationBtn.tag = 200;
    _operationBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_operationBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_operationBtn];
    UILabel *label1 = [UILabel LabelWithFont:BigFontSize andColor:NormalColor andTextAlignment:center andString:@"趋势图"];
    [self addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.left.mas_equalTo(self.mas_left);
    }];
    [_operationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.left.mas_equalTo(self.mas_left);
        make.height.mas_equalTo(self);
    }];
    
    _vehicelBtn = [UIButton bttonWithTitle:@"   " andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    _vehicelBtn.tag = 201;
    _vehicelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_vehicelBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_vehicelBtn];
    UILabel *label2 = [UILabel LabelWithFont:BigFontSize andColor:NormalColor andTextAlignment:center andString:@"行驶情况"];
    [self addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    [_vehicelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(self);
    }];
    
    _driveBtn = [UIButton bttonWithTitle:@"   " andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
    _driveBtn.tag = 202;
    _driveBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_driveBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label3 = [UILabel LabelWithFont:BigFontSize andColor:NormalColor andTextAlignment:center andString:@"报警情况"];
    [self addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.right.mas_equalTo(self.mas_right);
    }];
    [self addSubview:_driveBtn];
    [_driveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(kScreenWidth/3.0f);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(self);
    }];
    
//    _alarmBtn = [UIButton bttonWithTitle:@"报警情况" andNormalTitleColor:NormalColor selectColor:SelectColor titleFont:BigFontSize];
//    [_alarmBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_alarmBtn];
//    [_alarmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self);
//        make.width.mas_equalTo(kWidthOfPt(310));
//        make.left.mas_equalTo(self.mas_left).offset(kWidthOfPt(310)*3);
//    }];
    
    _lineView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth/3.0f-kScreenWidth*0.2f)*0.5, 142/3, kScreenWidth*0.2, 3)];
    _lineView.backgroundColor = SelectColor;
    [self addSubview:_lineView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}
- (void)btnDidClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 200;
//    if (sender.tag == 201) {
//        index = 0;
//    }else if ([sender.titleLabel.text isEqualToString:@"行驶情况"]){
//        index = 1;
//    }else if ([sender.titleLabel.text isEqualToString:@"报警情况"]){
//        index = 2;
//    }else{
//        index = 0;
//    }
    [self changeSelectItemWithIndex:index];
    if (_titleSelectIndex) {
        _titleSelectIndex(index);
    }
}
-(void)changeSelectItemWithIndex:(NSInteger)index
{
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.frame = CGRectMake((kScreenWidth/3.0f-kScreenWidth*0.2f)*0.5+kScreenWidth/3.0f*index, 142/3, kScreenWidth*0.2, kHeightOfPt(9));
    } completion:^(BOOL finished) {
//        CGFloat lineViewX = self.lineView.frame.origin.x;
//        CGFloat lineViewW = self.lineView.frame.size.width;
//        _operationBtn.selected = index==0;
//        _vehicelBtn.selected = index==1;
//        _driveBtn.selected = index==2;
//        _alarmBtn.selected = index==3;
//        if (lineViewX+lineViewW >= kScreenWidth) {
//            [self.titleScroller setContentOffset:CGPointMake(lineViewX+lineViewW - kScreenWidth, 0) animated:YES];
//        }else{
//            [self.titleScroller setContentOffset:CGPointMake(0, 0) animated:YES];
//        }
    }];
    
}

@end
